<template>
    <div>
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/test' }">问答</el-breadcrumb-item>
            <el-breadcrumb-item>官方</el-breadcrumb-item>
        </el-breadcrumb>
        <ListInfo v-for="item in qaData" :key="item.id" :title="item.title" :time="item.niceDate" :author="item.author" :checked="Boolean(item.checked)" :id="item.id"  @changeCheck="getCheck">
            <template #tags>
                <div class="list-tags">
                    <span>本站发布</span>
                    <span>问答</span>
                </div>
            </template>
            <template #kind>
                <div class="list-kind">
                    分类：
                    <span>问答</span> /
                    <span>官方</span>
                </div>
            </template>
        </ListInfo>
        <el-pagination
            background
            layout="prev, pager, next"
            :total="count"
            :page-size="23"
            @current-change="changePage"
        >
        </el-pagination>
    </div>
</template>

<script>
import ListInfo from '@/components/ListInfo.vue'
import { getQA, getCollect } from "@/api"
import mixin from "@/mixin"
export default {
    mixins: [mixin],
    name: "QA",
    components: {
        ListInfo
    },
    data(){
        return {
            qaData:[],
            collectData:[],
            num: 1,
            count: 0,
            index: 1
        }
    },
    created() {
        // 计算总条数
        this.getPageNum();
        // 获取初始数据（第一页）
        const p1 = getQA(this.index)
        const p2 = getCollect()
        Promise.all([p1,p2]).then(res=>{
            this.qaData = res[0].data.data.datas;
            this.collectData = res[1].data.data.datas;
            for(let i=0;i<this.qaData.length;i++){
                for(let j=0;j<this.collectData.length;j++){
                    if(this.qaData[i].id === this.collectData[j].originId){
                        this.qaData[i].checked = true;
                    }
                }
            }
        })
    },
    methods: {
        getPageNum(){
            getQA(this.num).then(res=>{
                if(res.data.data.datas.length > 0){
                    this.num++;
                    this.count += res.data.data.datas.length;
                    this.getPageNum();
                }
            })
        },
        changePage(i){
            getQA(i).then(res=>{
                this.qaData = res.data.data.datas;
            })
        }
    },
}
</script>

<style lang="less">
</style>